var reviewedRecordData = [];
var selectMonth = '\
    <select id="showMonth" name="showMonth" onchange="getMonthNum()" style="border: 0px;width: 50px;">\
	<option value="1">1月</option>\
	<option value="2">2月</option>\
	<option value="3">3月</option>\
	<option value="4">4月</option>\
	<option value="5">5月</option>\
	<option value="6">6月</option>\
	<option value="7">7月</option>\
	<option value="8">8月</option>\
	<option value="9">9月</option>\
	<option value="10">10月</option>\
	<option value="11">11月</option>\
	<option value="12">12月</option>\
</select>\
';
var flagDate = undefined;
var surer = undefined;
var indexFlag = undefined;
var lineData = undefined;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	getLineInfo();
	createTableHead("reviewRecordThead", reviewRecordTable);
	setTimeout(hideLoading, 500);
});
function getLineInfo(){
	var subBranchCompanyID = $("#subBranchCompanyID", parent.document).val();
	var teamID = $("#teamID", parent.document).val();
	$("#teamID").val(teamID);
	$("#subBranchCompanyID").val(subBranchCompanyID);
	monthChange(0);
	$ajaxList('lineName', getLineName, {subBranchCompanyID: subBranchCompanyID, teamID: teamID});
}
function getLineName(data) {
	var length = data.length;
	lineData = data;
	for (var i = 0; i < length; i++) {
		$("#lineID").append("<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
	}
}
function search(){
	$("#reviewDate").val($("#startChargingTime").val().substring(0,10));
	$ajax('searchReviewedRecord', 'lineReviewSearch', searchReviewedRecord);
}
function searchReviewedRecord(data) {
	if(data.result == "TeamIDNull") {
		showMessge("未分配所属车队，请联系管理员分配！");
	} else {
		reviewedRecordData = data && data.reviewedRecordList || [];
		clearTable("reviewRecordTbody");
		var tbody = $("#reviewRecordTbody");
		for(var i = 0; i < reviewedRecordData.length; i++) {
			$tr = $('<tr>').appendTo(tbody);
			$("<td>").html(i + 1).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].chargingStationName).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].subBranchCompanyName).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].teamName).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].lineName).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].chargingCount).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].sumChargingKWH).appendTo($tr);
			$("<td>").html(reviewedRecordData[i].reviewPerson).appendTo($tr);
			if(reviewedRecordData[i].reviewState == "1") {
				$('<td id="makesure'+ i +'">').html('<input id="surer' + i + '" value="' + window.parent.document.getElementById("userName").innerHTML + '" style="text-align:center;">').appendTo($tr);
				$('<td id="reviewState'+ i +'">').html('<span onclick="reviewState('+ i +')" class="bacLightGreen details operationWord" style="color: white;">确认</span>').appendTo($tr);
			} else {
				$("<td>").html(reviewedRecordData[i].surer).appendTo($tr);
				$("<td>").html("已确认").appendTo($tr);
			}
			if(reviewedRecordData[i].reviewWay == "charging") {
				$("<td>").html('<span onclick="details('+ i +')" class="bacHoverBlue details operationWord">详细</span>').appendTo($tr);
			} else {
				if(reviewedRecordData[i].reviewFile != null) {
					$("<td>").html('<span onclick="downLoad('+ i +')" class="bacLightGreen details operationWord" style="color: white;">查看</span>').appendTo($tr);
				} else {
					$("<td>").html("查阅手抄记录").appendTo($tr);
				}
			}
		}
	}
}
function getMonthNum(){
	var num = $("#showMonth").val();
	monthChange(parseInt(num));
}
function reviewState(index) {
	indexFlag = index;
	$ajax('updateManualReview', {ID: reviewedRecordData[index].id,
						Surer: $("#surer" + index).val(),
						}, updateData);
}
function updateData(data) {
	var result = data && data.result;
	if (result == "Success") {
		surer = $("#surer" + indexFlag).val();
		var data =  {
				SubBranchCompanyID : $("#subBranchCompanyID", parent.document).val(),
				TeamID : $("#teamID", parent.document).val(),
				LineID : reviewedRecordData[indexFlag].lineID,
				ChargingStationID : reviewedRecordData[indexFlag].chargingStationID,
				StartChargingDate : $("#startChargingTime").val(),
				EndChargingDate : $("#endChargingTime").val()
				};
		$ajax('updateRecordReviewState', data, updateRecordReviewState);
	} else if (result == "collectionNameEmpty"){
		showMessge(messageContent["Insert.Failed"]);
	} else if (result == "error"){
		showMessge(messageContent["Insert.Failed"]);
	}
}
function updateRecordReviewState(data){
	var result = data && data.result;
	if (result == "Success") {
		$("#reviewState" + indexFlag).html("已确认");
		$("#makesure" + indexFlag).html(surer);
	} else if (result == "SubBranchCompanyIDNull"){
		showMessge("未找到分公司！");
	} else if (result == "TeamIDNull"){
		showMessge("未找到车队！");
	} else if (result == "LineIDNull"){
		showMessge("未找到线路！");
	} else if (result == "StartChargingDateNull"){
		showMessge("时间错误！");
	} else if (result == "EndChargingDateNull"){
		showMessge("时间错误！");
	} else if (result == "NotFound"){
		showMessge("确认失败");
	} else {
		showMessge("确认异常");
	}
}
function details(index){
	$("#detailID").val(reviewedRecordData[index].subBranchCompanyID + ',' + reviewedRecordData[index].teamID + ',' + reviewedRecordData[index].lineID + ',' + $("#startChargingTime").val() + ',' + $("#endChargingTime").val() + ',' + reviewedRecordData[index].lineName);
	$("#iframeDetalis").show();
	$("#iframeDetalis").attr("src", "costManage/detailsRecord");
}
function downLoad(index) {
	indexFlag = index;
	showMessge("确定下载吗?", "model2");
}
function sure(){
	showMessge("下载中......");
	var url = 'downLoadReviewFile?fileName=' + reviewedRecordData[indexFlag].reviewFile + '&&reviewDate=' + reviewedRecordData[indexFlag].reviewDate + '&&chargingStationID=' + reviewedRecordData[indexFlag].chargingStationID +
	'&&checkFlag=';
	$ajax(url, undefined, exportFileResult);
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		var url = 'downLoadReviewFile?fileName=' + reviewedRecordData[indexFlag].reviewFile + '&&reviewDate=' + reviewedRecordData[indexFlag].reviewDate + '&&chargingStationID=' + reviewedRecordData[indexFlag].chargingStationID +
		'&&checkFlag=OK';
		downloadFile(url);
		showMessge("下载成功，文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "NotFound") {
		showMessge("下载失败，文件不存在!");
	} else {
		showMessge("下载失败，请重新下载 或 联系管理员！");
	}
}